package com.zu.commu.gsm1_0.handler;

import com.zu.commu.context.Config;
import com.zu.commu.gsm1_0.interf.Message;
import com.zu.commu.gsm1_0.interf.ReceiveHandler;
import com.zu.commu.interf.Action;
import com.zu.commu.interf.Data;
import com.zu.commu.interf.Order;
import com.zu.commu.interf.meter.Driver;
import com.zu.commu.interf.meter.Request;
import com.zu.commu.saveData.DataQueue;
import org.apache.log4j.Logger;

public class GReceiveHandler extends ReceiveHandler
{
  private static Logger log = Logger.getLogger("com.automic.commu.gsm1_0.handler.GReceiveHandler");

  public void handle(Message message)
  {
    Request request = getRequest(message.simNo);
    if (request == null) {
      return;
    }
    analyseData(request, message);
  }

  private Request getRequest(String gprsId)
  {
    Config cf = Config.instance();
    String protocol = cf.getMeterProcotolOfGPRSID(gprsId);
    if (protocol == null)
    {
      log.error("没有得到号码为" + gprsId + "的GSM短信协议。可能配置的sim卡号与实际的不相附合。");
      return null;
    }
    Driver driver = cf.getDriverOfMeterProtocol(protocol);
    return driver.getRequest();
  }

  public void analyseData(Request request, Message message)
  {
    Action action = request.analyseData(message.content.getBytes());

    if (action.equals(Action.SENDCENTER))
    {
      Data[] data = request.getCenterData();
      if ((data != null) && (data.length > 0)) {
        for (int i = 0; i < data.length; i++) {
          data[i].setGprsId(message.simNo);
        }
      }
      Order order = request.dataFromwhatOrder();
      if ((data != null) && (data.length > 0))
        sendCenter(data, order, message.simNo);
    }
    else if (action.equals(Action.SENDREMOTE))
    {
      sendRemote(message);
    } else if (action.equals(Action.SENDREMOTEANDCENTER))
    {
      sendRemote(message);

      Data[] data = request.getCenterData();
      if ((data != null) && (data.length > 0)) {
        for (int i = 0; i < data.length; i++) {
          data[i].setGprsId(message.simNo);
        }
      }
      Order order = request.dataFromwhatOrder();
      if ((data != null) && (data.length > 0))
        sendCenter(data, order, message.simNo);
    }
    else if (action.equals(Action.NOACTION))
    {
      log.info("没有动作。");
    } else if (action.equals(Action.ERROR))
    {
      log.error(request.getError());
    }
  }

  private void sendRemote(Message message)
  {
    if (message == null)
      log.error("出错！向远端模块发送短信内容为空。");
    try
    {
      GSendHandler lmp = new GSendHandler();
      lmp.handle(message);
    } catch (Exception e) {
      log.error("向远端发送短信出错 。", e);
    }
  }

  private void sendCenter(Data[] data, Order order, String gprsId)
  {
    String[] commandIds = (String[])null;
    Data temp = null;

    if (commandIds == null)
    {
      for (int i = 0; i < data.length; i++) {
        data[i].setGprsId(gprsId);
        DataQueue dq = DataQueue.instance();
        if ((order.equals(Order.CALL)) || 
          (order.equals(Order.COMMON)) || 
          (order.equals(Order.GSM))) {
          dq.setDataToSecondQueue(data[i]);
        }
        else {
          log.error("数据是命令结果，但找不到这个命令的ID（可能是等待超时，命令ID被清除），丢弃此结果数据！");
        }
      }
    }
    else
      for (int j = 0; j < commandIds.length; j++)
        for (int i = 0; i < data.length; i++) {
          temp = data[i];
          temp.setGprsId(gprsId);
          temp.setCommandId(commandIds[j]);
          DataQueue dq = DataQueue.instance();

          dq.setDataToFirstQueue(temp);
        }
  }
}